#include "stdafx.h"
#include "GUI.h"
#include "..\..\Global\globaldef.h"
#include "..\Common\Viewer.h"
#include "..\Common\Scene.h"


GUI::State GUI::Main::Viewer::g_MainState = GUI::State::CLOSED; 
std::vector<GUI::Components::Button> GUI::Main::Viewer::g_Buttons; 
//Render::tResource* GUI::Main::Viewer::g_pButtonSet = NULL;
GUI::tStamp GUI::Main::Viewer::m_MainStamp = {0,0,0,0};
D3DXVECTOR2 GUI::Main::Viewer::Position = D3DXVECTOR2(0,0);
float GUI::Main::Viewer::g_AnimateOffset = 0.0f;
Render::tResource* GUI::Main::Viewer::g_pBackground = NULL;

#define MAIN_ANIM_SPEED 0.45f

float GUI::Main::Viewer::AnimateDistance() {

	UINT h = 0;
	Render::Singleton()->GetTextureInfo(GUI::Main::Viewer::g_pBackground, NULL, &h);
	D3DXVECTOR2 t;
	GUI::ScaleToWindow(0,h, &t);

	return t.y;
}

void GUI::Main::Viewer::Update() {
	
	switch(GUI::Main::Viewer::g_MainState) {

		case GUI::OPENING:
		case GUI::CLOSING: {

			const float target = (GUI::Main::Viewer::g_MainState == GUI::OPENING) ? 0.0f : -GUI::Main::Viewer::AnimateDistance();
			float delta = (target - GUI::Main::Viewer::g_AnimateOffset) * MAIN_ANIM_SPEED;
			const float blip = 0.001f;

			GUI::Main::Viewer::g_AnimateOffset += delta;
			
			if((GUI::Main::Viewer::g_MainState == GUI::OPENING) && (GUI::Main::Viewer::g_AnimateOffset >= target - blip)) {
				
				delta = (target - GUI::Main::Viewer::g_AnimateOffset);

				GUI::Main::Viewer::g_MainState = GUI::OPEN;
				GUI::Main::Viewer::g_AnimateOffset = target;

				GUI::PokeMouse();
			}
			else if((GUI::Main::Viewer::g_MainState == GUI::CLOSING) && (GUI::Main::Viewer::g_AnimateOffset <= target + blip)) {
				
				delta = (target - GUI::Main::Viewer::g_AnimateOffset);
				
				GUI::Main::Viewer::g_MainState = GUI::CLOSED;
				GUI::Main::Viewer::g_AnimateOffset = target;
				GUI::PokeMouse();
			}

			FOREACH(GUI::Components::Button, i, g_Buttons)
				i->Position.y += delta;//Position.y + GUI::Main::Viewer::g_AnimateOffset;

	   }
	   break;
		
		case GUI::OPEN:
			//GUI::Main::Viewer::Update();

		case GUI::CLOSED:
			break;
	}

}



void GUI::Main::Viewer::OnToggle(void*, void*) {

	GUI::Main::Viewer::g_MainState = (GUI::Main::Viewer::g_MainState == GUI::State::CLOSED ||
		GUI::Main::Viewer::g_MainState == GUI::State::CLOSING) ?  GUI::State::OPENING : 
		GUI::State::CLOSING;
}

void OnExitConfirm(void* pCaller, void* pParam) {

	exit(0);
}

void OnResetConfirm(void* pCaller, void* pParam) {

	Scene::Release();
	GUI::Components::Dialog::OnClose(pCaller, pParam);
}

void GUI::Main::Viewer::OnExit(void*, void*) {

	GUI::Main::Viewer::OnToggle(NULL,NULL);
	Server* pServer;
	CViewer::GetServer(&pServer);
	uint clients = pServer->GetClientCount();

	char message[88 ];
	sprintf(message, "The are %i active client connections.\n\nAre you sure you want to quit?",clients);

	Components::Dialog *pDialogConfirm = new Components::Dialog();
	pDialogConfirm->Create("LiveView", message, OnExitConfirm,GUI::Components::Dialog::OnClose);
	pDialogConfirm->Position = D3DXVECTOR2(.5,.5);
	GUI::g_Dialogs.push_back(pDialogConfirm);
}
void GUI::Main::Viewer::OnReset(void*, void*) {

	GUI::Main::Viewer::OnToggle(NULL,NULL);
	Components::Dialog *pDialogConfirm= new Components::Dialog();
	pDialogConfirm->Create("LiveView", "Clicking OK will remove all objects within the scene.\n\nContinue?", OnResetConfirm,GUI::Components::Dialog::OnClose);
	pDialogConfirm->Position = D3DXVECTOR2(.5,.5);
	g_Dialogs.push_back(pDialogConfirm);
}

void GUI::Main::Viewer::Draw() {

	if(GUI::Main::Viewer::g_MainState != GUI::CLOSED) {
		
		D3DXVECTOR2 render_position = D3DXVECTOR2(0, g_AnimateOffset) + Position;
		Render::Singleton()->DrawSprite(GUI::Main::Viewer::g_pBackground, render_position, D3DXVECTOR2(1,1), NULL);
		
		FOREACH(GUI::Components::Button, i, g_Buttons)
			i->Draw();
	}
}

void GUI::Main::Viewer::OnMouseEvent(const float x, const float y, const GUI::MouseButton left, const GUI::MouseButton right) {
	
	switch(GUI::Main::Viewer::g_MainState) {

		case GUI::State::OPEN: {

			FOREACH(GUI::Components::Button, i, g_Buttons) {
				
				i->OnMouseEvent(x,y,left,right);
			}

		}break;
	}
}

bool GUI::Main::Viewer::Create() {

	Render::tResource* pBackground = NULL;

	try {

		if(!Render::Singleton()->CreateTexture("GUI\\lv_viewer.png", &pBackground))
			throw tException("Failed to create set texture");

		Release();

		for(uint i = 0; i < 4; i++) {

			GUI::Components::Button button = GUI::Components::Button();
			switch(i) {

				case 0:
					if(!button.Create("Reset")) continue;
					button.OnClick = GUI::Main::Viewer::OnReset;
					break;
				case 1:
					if(!button.Create("Exit")) continue;
					button.OnClick = GUI::Main::Viewer::OnExit;
					break;
				case 2:
					if(!button.Create("Load")) continue;
					button.OnClick = NULL;
					break;
				case 3:
					if(!button.Create("Save")) continue;
					button.OnClick = NULL;
					break;
			}

			GUI::ScaleToWindow((i < 2 ? 102 : 106) + i * 60, -14, &button.Position);
			GUI::Main::Viewer::g_Buttons.push_back(button);
		}

		GUI::Main::Viewer::g_MainState = GUI::State::CLOSED;
		GUI::Main::Viewer::g_pBackground = pBackground;
		GUI::ScaleToWindow(100, 22, &GUI::Main::Viewer::Position);
		GUI::Main::Viewer::g_AnimateOffset = -GUI::Main::Viewer::AnimateDistance();
		return true;

	}catch(tException e) {

		Render::Singleton()->ReleaseResource(&pBackground);
		Release();

		std::string message;
		tException::GetMessage(&e, message);
		return false;
	}
}

bool GUI::Main::Viewer::Release() {

	/*FOREACH(GUI::Components::Button, i, GUI::Main::Viewer::g_Buttons)
		i->Release();
	GUI::Main::Viewer::g_Buttons.clear();
	ZeroMemory(&GUI::Main::Viewer::m_MainStamp, sizeof(RECT));*/
	Render::Singleton()->ReleaseResource(&GUI::Main::Viewer::g_pBackground);
	return true;
}